#include "sharefile.h"
#include "tcpclient.h"
#include "opewidget.h"

ShareFile::ShareFile(QWidget *parent) : QWidget(parent)
{
    m_pSelectAllPB = new QPushButton("全选");
    m_pCancelSelectPB = new QPushButton("取消选择");

    m_pOKPB= new QPushButton("确定");
    m_pCancelPB= new QPushButton("取消");

    m_pSA = new QScrollArea;
    m_pFriendW = new QWidget;

    m_pButtonGroup = new QButtonGroup(m_pFriendW);
    m_pButtonGroup->setExclusive(false);//可以设计选择多个
    m_pFriendVBL = new QVBoxLayout(m_pFriendW);

    QHBoxLayout *pTopHBL = new QHBoxLayout;
    pTopHBL->addWidget(m_pSelectAllPB);
    pTopHBL->addWidget(m_pCancelSelectPB);
    pTopHBL->addStretch();

    QHBoxLayout *pDownHBL = new QHBoxLayout;
    pDownHBL->addWidget(m_pOKPB);
    pDownHBL->addWidget(m_pCancelPB);

    QVBoxLayout *pMainVBL = new QVBoxLayout;
    pMainVBL->addLayout(pTopHBL);
    pMainVBL->addWidget(m_pSA);
    pMainVBL->addLayout(pDownHBL);

    setLayout(pMainVBL);

    connect(m_pCancelSelectPB,SIGNAL(clicked(bool)),this,SLOT(cancelSelect()));//关联信号槽
    connect(m_pSelectAllPB,SIGNAL(clicked(bool)),this,SLOT(selectAll()));
    connect(m_pOKPB,SIGNAL(clicked(bool)),this,SLOT(okShare()));
    connect(m_pCancelPB,SIGNAL(clicked(bool)),this,SLOT(cancelShare()));





}

ShareFile &ShareFile::getinstance()
{
    static ShareFile instance;
    return instance;

}

void ShareFile::test()
{

    QVBoxLayout *p = new QVBoxLayout(m_pFriendW);

    QCheckBox *pCB = nullptr;
    for(int i = 0;i < 15;++i)
    {
        pCB = new QCheckBox("jack");
        p->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);

    }
    m_pSA->setWidget(m_pFriendW);

}

void ShareFile::updateFriend(QListWidget *pFriendList)
{
    if(pFriendList == nullptr) return;
    QAbstractButton *tmp = nullptr;
    QList<QAbstractButton*> preFriendList = m_pButtonGroup->buttons();
    for(int i = 0 ;i!=preFriendList.size();++i)
    {
        tmp = preFriendList[i];
        m_pFriendVBL->removeWidget(tmp);
        m_pButtonGroup->removeButton(tmp);;
        preFriendList.removeOne(tmp);
        delete tmp;
        tmp = nullptr;
    }
    QCheckBox *pCB = nullptr;
    for(int i = 0;i < pFriendList->count();++i)
    {
        pCB = new QCheckBox(pFriendList->item(i)->text());
        m_pFriendVBL->addWidget(pCB);
        m_pButtonGroup->addButton(pCB);

    }
    m_pSA->setWidget(m_pFriendW);


}

void ShareFile::cancelSelect()
{
    QList<QAbstractButton*> cbList =  m_pButtonGroup->buttons();//获得所有指针列表
    for(int i = 0;i<cbList.size();++i)
    {
        if(cbList[i]->isChecked())
        {
            cbList[i]->setChecked(false);
        }
    }
}

void ShareFile::selectAll()
{
    QList<QAbstractButton*> cbList =  m_pButtonGroup->buttons();//获得所有指针列表
    for(int i = 0;i<cbList.size();++i)
    {
        if(!cbList[i]->isChecked())
        {
            cbList[i]->setChecked(true);
        }
    }
}

void ShareFile::okShare()
{
    //先获得分享者
    QString strName = TcpClient::getInstance().loginName();
    QString strCurPath = TcpClient::getInstance().curPath();
    //获得将要选择的文件
    QString strShareFileName = OpeWidget::getInstance().getBook()->getShareFileName();//获得共享的文件名字
    QString strPath = strCurPath +"/" + strShareFileName;

    QList<QAbstractButton*> cbList =  m_pButtonGroup->buttons();//获得所有指针列表
    int num = 0;
    for(int i = 0;i<cbList.size();++i)
    {
        if(cbList[i]->isChecked())
        {
            num++;//用于获得有多少人被选择
        }
    }
    PDU *pdu = mkPDU(32*num+strPath.size()+1);
    pdu->uiMsgType = ENUM_MSG_TYPE_SHARE_FILE_REQUEST;//发出分享请求
    sprintf(pdu->caData,"%s %d",strName.toStdString().c_str(),num);
    int j = 0;
    for(int i = 0;i<cbList.size();++i)
    {
        if(cbList[i]->isChecked())
        {
            memcpy((char*)(pdu->caMsg) + j * 32,cbList[i]->text().toStdString().c_str(),cbList[i]->text().size());
            j++;
        }

    }
    memcpy((char*)(pdu->caMsg)+num*32,strPath.toStdString().c_str(),strPath.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
    free(pdu);
    pdu = nullptr;

}

void ShareFile::cancelShare()
{
    hide();
}
